%% 2Choice Task
fig = findobj('type','figure','-and','name','2ChoiceTaskResult');
if isempty(fig)
    fig = figure('name','2ChoiceTaskResult');
end
figure(fig)

rv = schedule.response_vals;

rcidx = regexp(schedule.readparams,'response_code');
rcidx = findincell(rcidx) + 1;
ttidx = regexp(schedule.readparams,'trial_type');
ttidx = findincell(ttidx) + 1;
hpidx = findincell(regexp(schedule.readparams,'HPFreq'));

vals   = rv(:,rcidx); vals(isnan(vals))     = [];
trials = rv(:,ttidx); trials(isnan(trials)) = [];
freqs  = rv(:,hpidx);  freqs(isnan(freqs))   = [];


% --- exclude first N trials ---
N = 0;
if N == 0 && length(vals) > 100
    N = length(vals)-100;
    vals(1:N)   = [];
    trials(1:N) = [];
elseif N > 0
    vals(1:N)   = [];
    trials(1:N) = [];
end
festr = sprintf('\t*%d trials excluded',N);
%----------------------------------------------------

hits = bitget(vals,6);

% AM State = 2
AMright = bitget(vals,4) & bitget(vals,13);
AMleft  = bitget(vals,3) & bitget(vals,13);

% Quiet State = 1
Qright = bitget(vals,4) & bitget(vals,12);
Qleft  = bitget(vals,3) & bitget(vals,12);

% Tone State = 0
Tright = bitget(vals,4) & bitget(vals,11);
Tleft  = bitget(vals,3) & bitget(vals,11);

amind = AMright|AMleft;
qind  = Qright|Qleft;
tind  = Tleft|Tright;

% totals
r = sum([AMright AMleft Qright Qleft Tleft Tright]);
AMhr = r(1)/sum(r([1 2]))*100;
Qhr  = r(3)/sum(r([3 4]))*100;
Thr  = r(5)/sum(r([5 6]))*100;
Ohr  = sum(hits|Qright)/length(hits)*100;

% print results to command window
fprintf('\n%s\n',schedule.schedule);
fprintf('\tHit\t\tMiss\tHR\n')
fprintf('AM\t%d\t\t%d\t\t%3.1f%%\n',r([1 2]),AMhr)
fprintf('Q\t%d\t\t%d\t\t%3.1f%%\n', r([3 4]),Qhr)
fprintf('T\t%d\t\t%d\t\t%3.1f%%\n', r([5 6]),Thr)
fprintf('#trials = %3.0f\t\t%3.1f%%%s\n\n',sum(r),Ohr,festr);


% plot by trials
% figure('windowstyle','docked','name',sprintf('Schedule: %s',schedule.schedule));
% set(gcf,'name',sprintf('Schedule: %s',schedule.schedule));
clf
subplot(10,1,1)
stairs(hits|Qright);
set(gca,'xtick',[],'ytick',[0 1],'yticklabel',{'miss' 'hit'}, ...
    'xlim',[0 length(hits)],'ylim',[-0.5 1.5]);

subplot(10,1,2:10)
hold on
grid on


amy = cumsum(AMright)./cumsum(amind) * 100;
qy  = cumsum(Qright) ./ cumsum(qind) * 100;
ty  = cumsum(Tleft)  ./ cumsum(tind) * 100;

plot(find(amind&AMright),amy(amind&AMright),'.b');
plot(find(qind&Qright),  qy(qind&Qright),   '.r');
plot(find(tind&Tleft),   ty(tind&Tleft),    '.g');

stairs(1:length(hits),cumsum(hits|Qright)'./(1:length(hits))*100,'-k')
% stairs(1:length(amind),cumsum(AMright|Qhits)./cumsum(amind|qind)*100,'-k')

plot(find(amind&AMleft),amy(amind&AMleft),'xb');
plot(find(qind&Qleft),  qy(qind&Qleft),   'xr');
plot(find(tind&Tright), ty(tind&Tright),  'xg');

plot([0 length(trials)],[50 50],'-k');

xlim([0 length(trials)]);
ylim([-10 110]);
set(gca,'ytick',[0 25 50 75 100]);
box on
xlabel('Trial number');
ylabel('Hit rate');
hold off
legend(sprintf('AM (%0.1f%%)',AMhr),sprintf('Quiet (%0.1f%%)',Qhr),sprintf('Tone (%0.1f%%)',Thr), ...
    'Overall','location','best')


% %% dprime
% 
% SignalTrials = bitget(vals,11) | bitget(vals,13);
% nSignalTrials = sum(SignalTrials);
% HR = sum(AMright | Tleft)/nSignalTrials;
% 
% CatchTrials = bitget(vals,12);
% nCatchTrials = sum(CatchTrials);
% FA = sum(Qright)/nCatchTrials;
% 
% [d,beta,c] = dprime(HR,FA,nSignalTrials,nCatchTrials);
% 
% fprintf('d'' = %0.3f\tbeta = %0.3f\tc = %0.3f\n',d,beta,c)






